package com.itsm.controller;

import com.itsm.entity.Approve;
import com.itsm.entity.Process;
import com.itsm.service.ApproveService;
import com.itsm.service.ProcessAllService;
import com.itsm.service.SendService;
import com.itsm.service.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
public class ApproveController {
    @Autowired
    private ApproveService approveService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SendService sendService;
    @Autowired
    private ProcessAllService processAllService;

    /**
     * 审批人获取审批的工单
     * @param processNo 工单号
     * @param request 审批人信息
     * @return 审批信息
     */
    @GetMapping("getApprove")
    public Approve getApprove(String processNo,HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        if(approveService.get1(processNo)!=null){
            if(approveService.get1(processNo).getStep()==2){
                return approveService.get1(processNo);
            }
        }
        return approveService.get(processNo,name);
    }

    /**
     * 按照提出人获取最新的工单
     * @param processNo 工单号
     * @param request 提出人姓名
     * @return 工单信息
     */
    @GetMapping("getApprove1")
    public Approve getApprove1(String processNo,HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        return approveService.get2(processNo,name);
    }

    /**
     * 更新审批表，判断是否需要创建下一个流程
     * @param approve
     * @param request
     * @return
     */
    @PostMapping("updateApprove")
    public Integer updateApprove(@RequestBody Approve approve, HttpServletRequest request) throws IOException {
        approve.setApproveTime(new Date());
        String name = (String) request.getSession().getAttribute("name");
        approve.setApprover(name);
        Integer col = approveService.updateApprove(approve);

        //审批退回
        if(approve.getStatus()==2){
            sendService.sendApprove(approve);
            return col;
        }
        //此步骤已经是IT主管审批，不生成下一审批流
        if(approve.getStep1()==null){
            return col;
        }
        //部门主管审批，生成IT主管审批流
        if(approve.getStep1()==2){
//            String itSup = userMapper.getItSup();
            approve.setStep(2);
            approve.setSubTime(new Date());
            approve.setApproveTime(null);
            approve.setStatus(0);
            approve.setId(null);
            approve.setStep1(null);
            approve.setApprover(null);
            sendService.sendIT(approve);
            approveService.addApprove(approve);
            return col;
        }
        if(approve.getStep1()==1){
            //判断是否需要两个领导审
            if(approveService.getCount(approve.getProcessNo())==2){
                //获取第二个审批
                Approve approve1 = approveService.select(approve.getProcessNo(),approve.getCount(),approve.getStep());
                approve1.setSubTime(new Date());
                approveService.updateApprove(approve1);
                Process process = processAllService.select(approve.getProcessNo());
                sendService.sendSup(process,approve1.getApprover());
            }else{
                approve.setStep(2);
                approve.setSubTime(new Date());
                approve.setApproveTime(null);
                approve.setStatus(0);
                approve.setId(null);
                approve.setStep1(null);
                approve.setApprover(null);
                sendService.sendIT(approve);
                approveService.addApprove(approve);
            }
//            approveService.updateApprove();
        }

        return col;
    }

    @GetMapping("approve1")
    public Approve getApprove1(String processNo){
        if(approveService.get3(processNo).size()==0){
            return null;
        }
        return approveService.get3(processNo).get(0);
    }
    @GetMapping("approve2")
    public Approve getApprove2(String processNo){
        if(approveService.get3(processNo).size()==0){
            return null;
        }
        if(approveService.get3(processNo).size()>=2){
            return approveService.get3(processNo).get(1);
        }
        return null;
    }

    @GetMapping("approve3")
    public Approve getApprove3(String processNo){
        if (approveService.get3(processNo).size()==3){
            return approveService.get3(processNo).get(2);
        }
        return null;
    }









}
